﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.ControlToolkit
{
    public enum DisplayTypes
    {
        None, //- The default datatype
        AlphabetsOnly, //Only Alphabets and Numbers
        AlphaNumeric, //Only Alphabets and Numbers
        Numeric, //Only natural numbers .. Leading zeros will be truncated (Int64)
        Currency, //$ with , and .00 is implied
        Digit, //Only whole Numbers .. Leading zeros will be shown (String) 
        DecimalOnly, //Numbers with decimal places
        Percent, //length is the number of positions: xxx.yyy% would be length 7
        Phone, //length = 7, format is xxx-xxxx
        PhoneWithArea, //length = 10, format is (xxx) xxx-xxxx
        Zip, //length is variable based on DATA
        SSN, //length = 9, format is xxx-xx-xxxx
        TaxId, //For Business Tax ID. Format is 99-9999999, length=9
        Custom,
        RestrictedCharacters, //Lets the user enter only the range of characters specified
        Email,
        PercentAuto  //The value passed is automically multiplied by 100 before displaying and divided by 100 before reading
    }

    public class MaskedBox : TextBox
    {

        public MaskedBox()
            : base()
        {
            this.KeyDown += new KeyEventHandler(MaskedBox_KeyDown);
        }

        void MaskedBox_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
            switch (DisplayType)
            {
                case DisplayTypes.Numeric:
                    e.Handled = !this.IsNumeric(e);
                    break;

                case DisplayTypes.Digit:
                    e.Handled = !this.IsDigit(e);
                    break;

                case DisplayTypes.DecimalOnly:
                    e.Handled = !this.IsDecimal(e);
                    break;

                default:
                    break;
            }


        }

        public static readonly DependencyProperty DisplayTypeProperty =
                DependencyProperty.Register("DisplayType", typeof(DisplayTypes), typeof(MaskedBox),
                        new PropertyMetadata(new PropertyChangedCallback(DisplayTypeChangedCallback)));

        private static void DisplayTypeChangedCallback(DependencyObject obj,
                                DependencyPropertyChangedEventArgs args)
        {
        }

        public DisplayTypes DisplayType
        {
            get
            {
                return (DisplayTypes)GetValue(DisplayTypeProperty);
            }

            set
            {
                SetValue(DisplayTypeProperty, value);

            }
        }





        private bool IsDigit(KeyEventArgs e)
        {
            if (((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)) && Keyboard.Modifiers == ModifierKeys.None)
                return true;
            else
                return false;

        }//end of method


        private bool IsNumeric(KeyEventArgs e)
        {

            if (this.IsDigit(e))
            {
                //If zero check that its not the first character being entered
                if (e.Key == Key.D0 || e.Key == Key.NumPad0)
                {
                    if ((this.SelectionStart <= 0 && this.SelectionLength < this.Text.Length) ||
                         (this.SelectionStart == 1 && this.Text.StartsWith("0")))
                        return false;
                    else
                        return true;

                }
                else
                    return true;

            }
            else
                return false;

        }//end of method

        private bool IsDecimalPoint(KeyEventArgs e)
        {

            //Check if decimal point was entered 
            if (e.Key == Key.Decimal && Keyboard.Modifiers == ModifierKeys.None)
            {
                //Ensure that there is no existing decimal 
                if (this.Text.Contains(".") && this.SelectionLength != this.Text.Length)
                    return false;
                else
                    return true;
            }
            else
                return false;


        }//end of method

        private bool IsDecimal(KeyEventArgs e)
        {

            if (this.IsNumeric(e) || this.IsDecimalPoint(e))
                return true;
            else
                return false;

        }//end of method

    } //MaskedBox

}
